package com.hjx.movie.core.system.controller.admin;

import com.hjx.movie.common.utils.Result;
import com.hjx.movie.common.utils.ResultType;
import com.hjx.movie.core.security.util.JwtAuthTokenUtil;
import com.hjx.movie.core.system.runner.SystemStaticData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/1/2 - 22:59
 */
@Slf4j
@RestController
@RequestMapping("/system")
public class SystemController {
    @Resource
    RedisTemplate<String,String> redisTemplate;
    @Resource
    JwtAuthTokenUtil jwtAuthTokenUtil;
    @Resource
    private SystemStaticData staticData;

    @RequestMapping("/clearRedisCache")
    public Result clearRedis(){
        Set<String> keys = Objects.requireNonNull(redisTemplate.keys("*")).stream()
                .filter(item -> !item.startsWith(jwtAuthTokenUtil.getHeader()))
                .collect(Collectors.toSet());
        log.debug(keys.toString());
        Long delete = redisTemplate.delete(keys);
        if (delete!=null&&delete>=1){
            return Result.success();
        }
        return Result.error(ResultType.SYSTEM_ERROR);
    }

    @GetMapping("getAllStaticData")
    public Result getAllStaticData(){
        return Result.success(staticData.getStaticDataList());
    }
}
